<template>
  <div>
    <div :id="code" />
  </div>
</template>

<script>
import { request } from "@/utils/request";
import checkPermission from "../../utils/permission";
import axios from "axios";
// import {registerFilter} from 'amis';

export default {
  name: "ScreenPage",
  data() {
    return {
      amisScoped: null
    };
  },
  computed: {
    code() {
      return this.$route.meta.amis;
    },
    userInfo() {
      return this.$store.getters["account/user"];
    },
  },
  watch: {
    code() {
      this.init();
    },
  },
  // 添加created,注册window全局函数,在labelPrintManage.json(打印功能)文件中调用
  created() {

  },
  methods: {
    init() {
      if (this.amisScoped) {
        this.amisScoped.unmount();
      }
      this.$nextTick(() => {
        // "@/assets/json/" + this.code + ".json"
        if(!this.code){
          return false
        }
        let path = `${process.env.BASE_URL}json/screen/${this.code}.json`
        axios.get(path).then(res=>{
          let amisJSON = res.data
          // this.permission(amisJSON);
          this.amisScoped = this.$amis.embed(
              "#" + this.code,
              amisJSON,
              {
                data: {
                  myData: 1,
                  addBtn: "新增",
                  detailBtn: "查看",
                  deleteBtn: "删除",
                  editBtn: "编辑",
                  enableBtn: "启用/禁用",
                  exportBtn: "导出"
                },
              },
              {
                // 可以不传，用来实现 ajax 请求
                fetcher: ({ url, method, data, config }) => {
                  if (!data) {
                    data = {};
                  }
                  data["tenantCode"] = sessionStorage.getItem("tenantCode");
                  data["loginUserId"] = this.userInfo.loginUserId;
                  return request(url, method, data, config);
                },
                theme: "antd",
              }
          );
        })
      });
    },

    permission(data) {
      for (let key in data) {
        if (typeof data[key] === "object") {
          if (Array.isArray(data[key])) {
            for (let i = 0; i < data[key].length; i++) {
              this.permission(data[key][i]);
            }
          } else {
            this.permission(data[key]);
          }
        } else {
          if (data["permission"]) {
            data["className"] = !checkPermission(this.code, data["permission"])
                ? "no-permission"
                : "";
          }
        }
      }
    },
  },
  mounted() {
    this.init();
  },
  destroyed() {
    console.log("==================");
    this.amisScoped.unmount();
  },
};

// registerFilter('arrayMerge', (aArray, bArray) =>
//     (aArray instanceof Array && bArray instanceof Array) ? aArray.push(bArray) : new Array()
// );
</script>
